/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.testdriver.parser;

import org.testis.data.parser.DynamicMapTreeProviderFactory;
import org.testis.orm.conditionhandler.ConditionHandlerFactory;
import org.testis.testdriver.ITestBuilder;
import org.testis.testdriver.TestPlanGlobalsHashMap;
import org.testis.testdriver.translation.AtofValueTranslateService;
import org.testis.testdriver.translation.UnidentifiedTranslationException;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Set;
import java.util.Vector;
import org.orm.PersistentException;
import org.testis.orm.refinedclasses.DynamicMapDesignTreeNode;
import org.testis.orm.refinedclasses.DynamicMapParameter;
import org.testis.orm.refinedclasses.TestDesignTreeNode;
import org.testis.orm.refinedclasses.TestSuiteRefined;

/**
 *
 * @author barisergun
 */
public abstract class TestDriverAbstractParsingStrategy {

    protected ITestBuilder m_builder;
    protected TestDesignTreeNode m_currentNode;
    protected String m_cloneString = "";

    public abstract void parseCurrentNode() throws PersistentException;

    protected String getFactoryIpFromDeviceId(String deviceId) throws
            PersistentException {

        DynamicMapDesignTreeNode rootNode =
                DynamicMapTreeProviderFactory.getInstance().
                getDynamicMapTree(deviceId);
        DynamicMapParameter factorySettingsIP = rootNode.getObject().
                getParameterByName("FactorySettingsIp");
        return factorySettingsIP.getReference().getValue();

    }

    protected String getCurrentNodePath() {

        TestDesignTreeNode temp = m_currentNode;

        String pathOfNode = temp.getName();
        do {
            temp = temp.getParent();
            pathOfNode = temp.getName() + "->" + pathOfNode;

        } while (!(temp.getObject() instanceof TestSuiteRefined));

        return pathOfNode;
    }

    protected LinkedList<String> getConditionsOfTestPart() {
        LinkedHashMap<Integer, String> conditionsMap =
                ConditionHandlerFactory.getInstance(m_currentNode).getConditions(m_currentNode);
        Collection<String> condColl = conditionsMap.values();

        LinkedList<String> condList = new LinkedList<String>(condColl);

        return condList;
    }

    protected void processMapStaticVariables(
            LinkedHashMap<String, String> dataToApply) {
        Set<String> allKeys = dataToApply.keySet();
        Collection<String> allValues = dataToApply.values();
        int collSize = allValues.size();
        Iterator<String> valueIter =
                allValues.iterator();
        Iterator<String> keyIter =
                allKeys.iterator();
        Vector<String> exceptions = new Vector<String>();
        for (int u = 0; u < collSize; u++) {
            String curValue = valueIter.next();
            String curKey = keyIter.next();

            try {
                AtofValueTranslateService service = new AtofValueTranslateService(
                        TestPlanGlobalsHashMap.createInstance().getTestplanglobals(),
                        TestPlanGlobalsHashMap.createInstance().getTestplanlanguages(),
                        TestPlanGlobalsHashMap.createInstance().getTestplandutvalues(),
                        null);

                curValue = service.translateStaticVariables(curValue);

                dataToApply.put(curKey,
                        curValue);
            } catch (UnidentifiedTranslationException eee) {
                exceptions.add(eee.getMessage());
            }

        }
        if (!exceptions.isEmpty()) {
            StringBuilder expMessage = new StringBuilder();
            for (int y = 0; y < exceptions.size(); y++) {
                expMessage.append(exceptions.get(y));
            }

            throw new UnidentifiedTranslationException(expMessage.toString());
        }
    }
}
